<?php




class User extends CI_Controller {

function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
        
       // $query = $this->db->get('entradas');
       //$this->load->helper('url');
       //$this->load->library('form_validation');
    }
	function index(){
		$this->load->view('login_view');
	}
	function m()
	{
		/*//This assumes you used the sample MySQL table
		$user_table = 'users';
		//Load the URL helper
		$this->load->helper('url');
		$this->load->library('form_validation');
		/*
		//Uncomment this if you are using OBSession
		if($this->session->flashdata('error')) {
			echo '<div class="error" style="background-color:red;">';
				echo $this->session->flashdata('error_text');
			echo '</div>';
		} elseif($this->session->flashdata('success')) {
			echo '<div class="success" style="background-color:green;">';
				echo $this->session->flashdata('success_text');
			echo '</div>';		
		}
		*/


		//BOF Status Info
		/*echo '<div id="status">';
			echo '<h3>User Status</h3>';
			if($this->session->userdata('logged_in')) {
				echo 'User logged in as ' . $this->session->userdata('username');
			} else {
				echo 'User not logged in';
			}
		echo '</div>';
		echo '<hr />';
		//EOF Status Info

		//BOF Create user
		echo '<div id="create">';
			echo '<h3>Create A User</h3>';
			echo '<form action="' . site_url('/example/create/') . '" method="post">';
				
				echo '<label for="create_username">Username:</label>';
				echo '<input type="text" id="create_username" name="create_username" value="" /><br />';

				echo '<label for="create_password">Password:</label>';
				echo '<input type="password" id="create_password" name="create_password" value="" /><br />';

				echo '<input type="submit" id="create" name="create" value="Create" />';

			echo '</form>';
		echo '</div>';
		echo '<hr />';
		//EOF Create user

		
		//BOF Login user
		if(!$this->session->userdata('logged_in')) {
			echo '<div id="login">';
				echo '<h3>Login</h3>';
				echo '<form action="' . site_url('/example/login/') . '" method="post">';
					
					echo '<label for="login_username">Username:</label>';
					echo '<input type="text" id="login_username" name="login_username" value="" /><br />';
	
					echo '<label for="login_password">Password:</label>';
					echo '<input type="password" id="login_password" name="login_password" value="" /><br />';
	
					echo '<input type="submit" id="login" name="login" value="Login" />';
	
				echo '</form>';
			echo '</div>';
			echo '<hr />';
		} else {
			echo '<div id="logut">';
				echo '<h3>Logut</h3>';
				echo '<a href="' . site_url('/example/logout/') . '">Click here to logout.</a>';
			echo '</div>';
			echo '<hr />';
			
		}
		//EOF Login user
		
		//BOF User table
		if($this->session->userdata('logged_in')) {
			//Grab user data from database
			$query = $this->db->select('id, username');
			$query = $this->db->get($user_table);
			$user_array = $query->result_array();
			
			if(count($user_array) > 0) {
				echo '<div id="user_table">';
					echo '<h3>User Table</h3>';
					echo '<table>';
						echo '<tr>';
							echo '<th>';
								echo 'ID';
							echo '</th>';
							echo '<th>';
								echo 'Username';
							echo '</th>';
							echo '<th>';
								echo 'Delete';
							echo '</th>';
						echo '</tr>';
						foreach($user_array as $ua) {
							echo '<tr>';
								echo '<td>';
									echo $ua['id'];
								echo '</td>';
								echo '<td>';
									echo $ua['username'];
								echo '</td>';
								echo '<td>';
									echo '<a href="' . site_url('/example/delete/' . $ua['id']) . '" onclick="return confirm(\'Are you sure you want to delete this user?\')">Delete</a>';
								echo '</td>';
							echo '</tr>';
						}
					echo '</table>';
				echo '</div>';
				echo '<hr />';
			}
		}
		//EOF User table
		*/
	}
	
	function create()
	{
		//Load
		/*$this->load->helper('url');
		$this->load->library('validation');
		
		//Check incoming variables
		$rules['create_username']	= "required|min_length[4]|max_length[32]|alpha_dash";
		$rules['create_password']	= "required|min_length[4]|max_length[32]|alpha_dash";		

		$this->validation->set_rules($rules);

		$fields['create_username'] = 'Username';
		$fields['create_password'] = 'Password';
		
		$this->validation->set_fields($fields);
				
		if ($this->validation->run() == false) {
			/*
			//If you are using OBSession you can uncomment these lines
			$flashdata = array('error' => true, 'error_text' => $this->validation->error_string);
			$this->session->set_flashdata($flashdata); 
			$this->session->set_flashdata($_POST);
			
			redirect('/example/');			
		} else {
			//Create account
			if($this->simplelogin->create($this->input->post('create_username'), $this->input->post('create_password'))) {
				/*
				//If you are using OBSession you can uncomment these lines
				$flashdata = array('success' => true, 'success_text' => 'Account Creation Successful!');
				$this->session->set_flashdata($flashdata);
				
				redirect('/example/');	
			} else {
				/*
				//If you are using OBSession you can uncomment these lines
				$flashdata = array('error' => true, 'error_text' => 'There was a problem creating the account.');
				$this->session->set_flashdata($flashdata); 
				$this->session->set_flashdata($_POST);
				
				redirect('/example/');			
			}			
		}*/
	}

	function delete($user_id)
	{
		
		
		//Load
		/*$this->load->helper('url');

		if($this->simplelogin->delete($user_id)) {
			/*
			//If you are using OBSession you can uncomment these lines
			$flashdata = array('success' => true, 'success_text' => 'Deletion Successful!');
			$this->session->set_flashdata($flashdata);
			
			redirect('/example/');	
		} else {
			/*
			//If you are using OBSession you can uncomment these lines
			$flashdata = array('error' => true, 'error_text' => 'There was a problem creating the account.');
			$this->session->set_flashdata($flashdata); 
			$this->session->set_flashdata($_POST);
			
			redirect('/example/');			
		}	*/		
		
	}
    function logout(){
    	$this->session->set_userdata('validation',FALSE);
    	$this->session->set_userdata('logged_in',FALSE); 
	    $this->load->view('login_view');
    }
	function login()
	{
		$this->load->helper('url');
		$this->load->library('form_validation');
		//Check incoming variables
		$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|max_length[100]|xss_clean');
				
		if ($this->form_validation->run() == false) {
			$this->load->view('login_view');			
		} else {
			$username=$this->input->post('username');
			$password=$this->input->post('username');
			
			$id=$this->User_Model->check_login($username,$password);
			if (! $id){
				//login failure
				$this->session->set_flashdata('login_error',TRUE);
				$this->load->view('login_view');
			}else{
				//logged
				$this->session->set_flashdata('login_error',FALSE);
				$this->session->set_userdata('logged_in',TRUE);
				$this->session->set_userdata('user_id',$id);
				$this->session->set_userdata('username',$username);
				$this->session->set_userdata('validation',TRUE);
				$this->load->view('user_view');
			}		
	}
	}
}
?>